package com.example.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.contant.MessageConstant;
import com.example.entity.PageResult;
import com.example.entity.QueryPageBean;
import com.example.entity.Result;
import com.example.pojo.AddressAdmin;
import com.example.pojo.Goods;
import com.example.pojo.OrderAdmin;
import com.example.service.OrderAdminService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrdeAdminController {
    @Reference
    private OrderAdminService orderAdminService;
    @PostMapping("findpage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=orderAdminService.findpage(queryPageBean);
        List<OrderAdmin> rows = pageResult.getRows();
        if (rows!=null && rows.size()>0)
        {
            for (OrderAdmin row : rows) {
                AddressAdmin addressAdmin = row.getAddressAdmin();
                if (addressAdmin!=null){
                    row.setAddress(addressAdmin.toString());
                }

            }
        }

        pageResult.setRows(rows);
        return pageResult;
    }
    @GetMapping("/delete")
    public Result delete(Integer id){

        try{
            orderAdminService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false, MessageConstant.DELETE_ORDERADMIN_FAIL);
        }
        return new Result(true,MessageConstant.DELEET_ORDERADMIN_SUCCESS);
    }
    @GetMapping("/findById")
    public Result findById(Integer id){
        try{
            OrderAdmin orderAdmin=orderAdminService.findById(id);
            return  new Result(true,MessageConstant.QUERY_ORDERADMIN_SUCCESS,orderAdmin);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDERADMIN_FAIL);
        }
    }
    @PostMapping("/edit")
    public Result edit(@RequestBody OrderAdmin orderAdmin, Integer[] id){
        orderAdmin.setAddrId(Integer.parseInt(id[0].toString()));
        System.out.println(orderAdmin);
        try{
            orderAdminService.edit(orderAdmin);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ORDERADMIN_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_ORDERADMIN_SUCCESS);

    }
}
